package com.hgcode.platform.util;

import com.google.common.base.Splitter;
import com.hgcode.platform.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.util.CollectionUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class CollectionUtil {

    @Test
    public void test01() {
        User user = new User();
        user.setId(123L);
        user.setName("wentao");
        user.setAge(12);
        List<User> userList = new ArrayList<>();
        userList.add(user);

        userList.stream().forEach(x -> x.setId(null));
        userList.stream().forEach(System.out::println);

    }

    @Test
    public void test02() throws UnsupportedEncodingException {
        String str="1;A；b";
        List<String> list=Splitter.onPattern("[;|；]")
                .omitEmptyStrings()
                .splitToList(str);
        for(String s : list){
            System.out.println(s);
        }
    }

    @Test
    public void test03() throws UnsupportedEncodingException {
        List list=new ArrayList();
        list.add(null);
        list.add(null);
        System.out.println(CollectionUtils.isEmpty(list));
    }

    @Test
    public void test04() throws UnsupportedEncodingException {
        List<User> userList=new ArrayList<>();
       /* User user=new User(1L,"wentao",12);
        User user2=new User(2L,"wentao",12);
        User user3=new User(3L,"wentao",12);
        userList.add(user);
        userList.add(user2);
        userList.add(user3);*/

        List<User> result=userList.stream().filter(x->{
            if(x.getId().equals(2L) || x.getId().equals(1L)){
                return false;
            }
            return true;
        }).collect(Collectors.toList());
        System.out.println(result.size());
    }

    @Test
    public void test05() throws UnsupportedEncodingException {
        ThreadLocal<Integer> t=new ThreadLocal<>();
        System.out.println(t.get());
    }

    @Test
    public void test06() throws UnsupportedEncodingException {
        System.out.println(StringUtils.isNumeric("115 86"));
    }

    @Test
    public void test07() {
        List<User> list=new ArrayList<>();
        User user=new User();
        list.add(user);
        List<User> result=list.stream().filter(x->{
            if(x.getId()==null){
                return false;
            }
            return true;
        }).collect(Collectors.toList());
        System.out.println(result.size());
        List<User> s=result.stream().filter(distinctByKey(o -> o.getId())).collect(Collectors.toList());

    }

    @Test
    public void test09() {
        List<User> list=new ArrayList<>();


        List<User> result=list.stream().filter(x->x.getId().equals(123L)).collect(Collectors.toList());
        System.out.println(result.size());

    }

    @Test
    public void test08() {
        System.out.println(Integer.valueOf(1).equals(1));
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    @Test
    public void test10() {

        Set<Integer> set=new HashSet<>();
        set.add(500);
        set.add(700);
        System.out.println(set.contains(500));

    }
}
